Skip to content

Add OutputScope for output message tracing with parent span linking#164

Merged
nikhilNava merged 11 commits intomainfrom
copilot/extract-output-span-changes
Feb 9, 2026
Merged

Add OutputScope for output message tracing with parent span linking#164
nikhilNava merged 11 commits intomainfrom
copilot/extract-output-span-changes

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

Extracts output span functionality from PR #113. Adds OutputScope for tracing agent output messages with W3C Trace Context parent span linking.

New files

  • spans_scopes/output_scope.py - OpenTelemetry scope for output messages
  • models/response.py - Response dataclass

Base scope changes

  • Added parent_id parameter to OpenTelemetryScope.__init__
  • Parent context parsed from W3C format and passed at span creation (OTel Python has no post-creation parent setter)

Utils refactoring

  • parse_parent_id_to_context() moved to utils.py
  • Added validate_trace_id(), validate_span_id() with hex validation

Behavior

  • record_output_messages() appends to accumulated list (not replace)
response = Response(messages=["Hello"])
parent_id = "00-1234567890abcdef1234567890abcdef-abcdefabcdef1234-01"

with OutputScope.start(agent_details, tenant_details, response, parent_id=parent_id) as scope:
    scope.record_output_messages(["More"])  # Result: ["Hello", "More"]

Tests

Reduced from 10 to 4 focused tests using real spans via InMemorySpanExporter.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • il-1234567812345678123456781234567.8.tenant.api.powerplatform.com
    • Triggering command: /home/REDACTED/work/Agent365-python/Agent365-python/.venv/bin/pytest pytest tests/observability/core/ -v --tb=short -m not integration (dns block)
  • il-testtenan.t.tenant.api.powerplatform.com
    • Triggering command: /home/REDACTED/work/Agent365-python/Agent365-python/.venv/bin/pytest pytest tests/observability/core/ -v --tb=short -m not integration (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 6, 2026 15:19
- Add Response model class for agent execution response details
- Add spans_scopes directory with OutputScope implementation
- Add unit tests for OutputScope functionality

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title [WIP] Extract changes to output span from existing PR Add OutputScope for output message tracing Feb 6, 2026
Copilot AI requested a review from nikhilNava February 6, 2026 15:21
Similar to the .NET ExecuteToolScope, the OutputScope now accepts an optional
parent_id parameter to link spans to upstream operations.

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope for output message tracing Add OutputScope with parent_id for output message tracing Feb 6, 2026
Similar to the .NET implementation, the base OpenTelemetryScope class now
accepts an optional parent_id parameter that sets the CUSTOM_PARENT_SPAN_ID_KEY
attribute on the span. This allows all scope subclasses to support parent
span linking consistently.

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope with parent_id for output message tracing Add OutputScope with parent_id support on base OpenTelemetryScope Feb 6, 2026
Changed the behavior of record_output_messages to append new messages to
the accumulated list rather than replacing all messages. This allows
collecting output messages over multiple calls during the scope's lifetime.

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope with parent_id support on base OpenTelemetryScope Add OutputScope for output message tracing with parent_id support Feb 6, 2026
Changed the parent_id implementation to properly link spans by parsing the
W3C Trace Context format parent ID and using it to create a proper span
context. This aligns with the .NET implementation which calls
`activity?.SetParentId(parentId!)`.

Added validation for W3C Trace Context version and trace_id/span_id lengths.

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope for output message tracing with parent_id support Add OutputScope for output message tracing with proper parent span linking Feb 6, 2026
Copilot AI changed the title Add OutputScope for output message tracing with proper parent span linking Add OutputScope for output message tracing with parent span linking Feb 6, 2026
Refactored W3C Trace Context parsing:
- Added W3C Trace Context validation constants
- Added validate_w3c_trace_context_version() helper
- Added validate_trace_id() helper with hex validation
- Added validate_span_id() helper with hex validation
- Moved parse_parent_id_to_context() from opentelemetry_scope.py

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope for output message tracing with parent span linking Add OutputScope for output message tracing with W3C parent span linking Feb 6, 2026
…similar ones

Reduced from 10 tests to 4 focused tests:
- test_output_scope_creates_span_with_messages (merged span name and messages tests)
- test_record_output_messages_appends (merged multiple append tests)
- test_output_scope_with_parent_id (parent linking)
- test_output_scope_dispose (manual dispose)

All tests use real spans via InMemorySpanExporter (no mocks).

Co-authored-by: nikhilNava <[email protected]>
Copilot AI changed the title Add OutputScope for output message tracing with W3C parent span linking Add OutputScope for output message tracing with parent span linking Feb 6, 2026
@nikhilNava nikhilNava marked this pull request as ready for review February 9, 2026 13:31
@nikhilNava nikhilNava requested a review from a team as a code owner February 9, 2026 13:31
Copilot AI review requested due to automatic review settings February 9, 2026 13:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a dedicated OutputScope to the observability core so agent output messages can be traced as their own OpenTelemetry span, with optional W3C Trace Context (traceparent) parent span linking.

Changes:

  • Introduces OutputScope for recording/appending output messages onto a span attribute.
  • Adds Response dataclass and extends OpenTelemetryScope to accept parent_id, parsing it into an OTel parent context at span creation time.
  • Refactors W3C parent parsing into utils.py, adding basic hex/id validation helpers, and adds focused unit tests using InMemorySpanExporter.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
tests/observability/core/test_output_scope.py New unit tests covering span creation, append behavior, and parent linking for OutputScope.
.../observability/core/utils.py Adds W3C traceparent parsing + validation helpers and returns an OTel context containing a remote parent span.
.../observability/core/spans_scopes/output_scope.py New OutputScope implementation that sets/appends gen_ai.output.messages.
.../observability/core/spans_scopes/__init__.py Introduces the package init for spans_scopes.
.../observability/core/opentelemetry_scope.py Adds parent_id support by using parsed parent context when starting spans.
.../observability/core/models/response.py New Response dataclass for output message payload.

juliomenendez
juliomenendez previously approved these changes Feb 9, 2026
@nikhilNava nikhilNava merged commit eaa2136 into main Feb 9, 2026
14 checks passed
@nikhilNava nikhilNava deleted the copilot/extract-output-span-changes branch February 9, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants